package com.ct.ycz_schedule.start;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.log.StaticLog;
import com.ct.ycz_schedule.ScheduleManager;
import com.ct.ycz_schedule.task.RedisDataTask;
import com.ct.ycz_schedule.task.UserVipTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
//随着 springboot 容器启动而启动
public class RedisDataTaskStart implements CommandLineRunner {
    @Autowired
    private ScheduleManager scheduleManager;

    @Autowired
    private RedisDataTask redisDataTask;
    @Autowired
    private UserVipTask userVipTask;

    private static final String redisKey = IdUtil.randomUUID();
    private static final String userVipKey = IdUtil.randomUUID();


    @Override
    public void run(String... args) throws Exception {
        StaticLog.info("RedisDataTaskStart");
        //定时任务开启
        scheduleManager.startTask(redisKey, redisDataTask, "*/3 * * * * ?");
        scheduleManager.startTask(userVipKey, userVipTask, "*/3 * * * * ?");
        ThreadUtil.sleep(1000);
        //改变定时任务的cron表达式
        scheduleManager.changeTask(redisKey, "*/5 * * * * ?");
        ThreadUtil.sleep(1000);
        //定时任务停止
        scheduleManager.stopTask(redisKey);
    }
}
